package com.ninogame.state;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParserException;

import com.ninogame.BitmapFont;
import com.ninogame.Dialog;
import com.ninogame.GameLayer;
import com.ninogame.GameLib;
import com.ninogame.IConstant;
import com.ninogame.MazeEscape;
import com.ninogame.Map;
import com.ninogame.Actor.Creature;
import com.ninogame.Actor.Explorer;
import com.ninogame.resolution.DEF;


import android.R.color;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.view.KeyEvent;


public class StateSelectLevel extends MazeEscape
{
	public static RectF rectF;
	public static int NUM_ROW = 5;
	public static int NUM_COL = 4;
	public static int MAX_PAG = 3;
	public static int mcurrentPage = 0;
	public static int SELECTLEVEL_BUTTON_W = 0;
	public static int SELECTLEVEL_BUTTON_H = 0;	
	public static int SELECTLEVEL_BEGIN_Y = 0;
	public static int SELECTLEVEL_BEGIN_X = 0;
	public static synchronized void SendMessage(int type)
	{

		switch (type) 
		{
		case MESSAGE_CTOR: 
			rectF = new RectF(DEF.SELECTLEVEL_BACKGROUND_X, DEF.SELECTLEVEL_BACKGROUND_Y, DEF.SELECTLEVEL_BACKGROUND_X + DEF.SELECTLEVEL_BACKGROUND_W, DEF.SELECTLEVEL_BACKGROUND_Y + DEF.SELECTLEVEL_BACKGROUND_H);
			mcurrentPage = StateGameplay.s_UnlockLevel / (NUM_ROW * NUM_COL);
			
			SELECTLEVEL_BUTTON_H = StateGameplay.spriteDPad.getFrameHeight(DEF.FRAME_SELECTLEVEL_NORMAL);
			SELECTLEVEL_BUTTON_W = StateGameplay.spriteDPad.getFrameWidth(DEF.FRAME_SELECTLEVEL_NORMAL);
			
			SELECTLEVEL_BEGIN_Y = (SCREEN_HEIGHT - ((SELECTLEVEL_BUTTON_H + DEF.SELECTLEVEL_CONTENT_SPACE_H)* (NUM_ROW -1)))/2 + 20;
			SELECTLEVEL_BEGIN_X = (SCREEN_WIDTH - ((SELECTLEVEL_BUTTON_W + DEF.SELECTLEVEL_CONTENT_SPACE_W)* (NUM_COL-1)))/2;
			
			break;
		case MESSAGE_UPDATE:
			for (int i = 0; i < NUM_ROW; i++) {
				int y = SELECTLEVEL_BEGIN_Y + i * (SELECTLEVEL_BUTTON_H + DEF.SELECTLEVEL_CONTENT_SPACE_H);
				int x = 0;
				for (int j = 0; j < NUM_COL; j++) {
					x = SELECTLEVEL_BEGIN_X + j * (SELECTLEVEL_BUTTON_W + DEF.SELECTLEVEL_CONTENT_SPACE_W);

					if (isTouchReleaseInRect(x - SELECTLEVEL_BUTTON_W / 2, y - SELECTLEVEL_BUTTON_H / 2, SELECTLEVEL_BUTTON_W, SELECTLEVEL_BUTTON_H)) {

						StateGameplay.m_CurrentLevel = mcurrentPage * NUM_COL * NUM_ROW + i * NUM_COL + j + 1;
						if (StateGameplay.m_CurrentLevel <= StateGameplay.s_UnlockLevel)
							changeState(STATE_GAMEPLAY);
						break;
					}

				}
			}

			//left right button
			if (MazeEscape.isTouchReleaseInRect(DEF.SELECTLEVEL_BACKGROUND_X + 5, SCREEN_HEIGHT / 2, DEF.DPAD_WIDTH, DEF.DPAD_HEIGHT)) {
				mcurrentPage--;
				if (mcurrentPage < 0)
					mcurrentPage = MAX_PAG - 1;
			}

			//right	
			if (MazeEscape.isTouchReleaseInRect(SCREEN_WIDTH - (DEF.SELECTLEVEL_BACKGROUND_X + 5 + DEF.DPAD_WIDTH), SCREEN_HEIGHT / 2, DEF.DPAD_WIDTH, DEF.DPAD_HEIGHT)) {
				mcurrentPage++;
				if (mcurrentPage >= MAX_PAG)
					mcurrentPage = 0;
			}

			if (MazeEscape.isTouchReleaseInRect(DEF.SELECTLEVEL_BACKGROUND_X + DEF.SELECTLEVEL_BACKGROUND_W - SELECTLEVEL_BUTTON_W - 8, DEF.SELECTLEVEL_BACKGROUND_Y + 8, SELECTLEVEL_BUTTON_W, SELECTLEVEL_BUTTON_H))
				changeState(STATE_MAINMENU, true, true);
			//if (GameLib.isTouchReleaseScreen()) {
			//	changeState(STATE_GAMEPLAY);
			//}

			break;
		case MESSAGE_PAINT:
			if (!StateGameplay.isIngame) {
				MazeEscape.mainPaint.setColor(Color.BLACK);
				MazeEscape.mainPaint.setStyle(Style.FILL);
				mainCanvas.drawRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, MazeEscape.mainPaint);
				matrix.reset();
				matrix.setScale(SCREEN_WIDTH * 1f / 800, SCREEN_HEIGHT * 1f / 1280);
				mainCanvas.drawBitmap(StateMainMenu.splashBitmap, matrix, mainPaint);
			} else {
				StateGameplay.SendMessage(MESSAGE_PAINT);
			}
			MazeEscape.mainPaint.setStyle(Style.FILL);
			MazeEscape.mainPaint.setARGB(100, 0, 0, 0);
			MazeEscape.mainCanvas.drawRect(0, 0, MazeEscape.SCREEN_WIDTH, MazeEscape.SCREEN_HEIGHT, MazeEscape.mainPaint);
			MazeEscape.mainPaint.setStyle(Style.FILL_AND_STROKE);
			MazeEscape.mainPaint.setARGB(180, 67, 120, 167);
			//MainActivity.mainPaint.setColor(Color.rgb(67, 120, 167));

			MazeEscape.mainCanvas.drawRoundRect(rectF, 25, 25, MazeEscape.mainPaint);//DEF.SELECTLEVEL_TITLE_CONTENT_SPACE_H
			//draw text
			MazeEscape.fontbig_Yellow.drawString(MazeEscape.mainCanvas, "SELECT LEVEL", DEF.SELECTLEVEL_TITLE_X, DEF.SELECTLEVEL_TITLE_Y, BitmapFont.ALIGN_CENTER);

			for (int i = 0; i < NUM_ROW; i++) {
				int y = SELECTLEVEL_BEGIN_Y + i * (SELECTLEVEL_BUTTON_H + DEF.SELECTLEVEL_CONTENT_SPACE_H);
				int x = 0;
				for (int j = 0; j < NUM_COL; j++) {
					x = SELECTLEVEL_BEGIN_X + j * (SELECTLEVEL_BUTTON_W + DEF.SELECTLEVEL_CONTENT_SPACE_W);
					StateGameplay.m_CurrentLevel = mcurrentPage * NUM_COL * NUM_ROW + i * NUM_COL + j + 1;
					if (StateGameplay.m_CurrentLevel <= StateGameplay.s_UnlockLevel) {
						if (isTouchDrapInRect(x - SELECTLEVEL_BUTTON_W / 2, y - SELECTLEVEL_BUTTON_H / 2, SELECTLEVEL_BUTTON_W, SELECTLEVEL_BUTTON_H))
							StateGameplay.spriteDPad.drawAFrame(mainCanvas, DEF.FRAME_SELECTLEVEL_HIGHTLIGHT, x, y);
						else
							StateGameplay.spriteDPad.drawAFrame(mainCanvas, DEF.FRAME_SELECTLEVEL_NORMAL, x, y);
						fontbig_White.drawString(mainCanvas, " " + (mcurrentPage * NUM_COL * NUM_ROW + i * NUM_COL + j + 1) + " ", x, y - 30, BitmapFont.ALIGN_CENTER);
						if (StateGameplay.m_CurrentLevel != StateGameplay.s_UnlockLevel)
							
						fontbig_White.drawString(mainCanvas, "Pass", x, y + 6 , BitmapFont.ALIGN_CENTER);
						else
							fontbig_Yellow.drawString(mainCanvas, "New", x, y + 6 , BitmapFont.ALIGN_CENTER);
					} else {
						StateGameplay.spriteDPad.drawAFrame(mainCanvas, DEF.FRAME_SELECTLEVEL_LOCK, x, y);
					}

				}
			}
			//select left and right button
			if (MazeEscape.isTouchDrapInRect(DEF.SELECTLEVEL_BACKGROUND_X + 5, SCREEN_HEIGHT / 2, DEF.DPAD_WIDTH, DEF.DPAD_HEIGHT))
				StateGameplay.spriteDPad.drawAFrame(MazeEscape.mainCanvas, DEF.FRAME_BUTTON_LEFT_NORMAL, DEF.SELECTLEVEL_BACKGROUND_X + 5, SCREEN_HEIGHT / 2);
			else
				StateGameplay.spriteDPad.drawAFrame(MazeEscape.mainCanvas, DEF.FRAME_BUTTON_LEFT_HIGHTLIGHT, DEF.SELECTLEVEL_BACKGROUND_X + 5, SCREEN_HEIGHT / 2);
			//right	
			if (MazeEscape.isTouchDrapInRect(SCREEN_WIDTH - (DEF.SELECTLEVEL_BACKGROUND_X + 5 + DEF.DPAD_WIDTH), SCREEN_HEIGHT / 2, DEF.DPAD_WIDTH, DEF.DPAD_HEIGHT))
				StateGameplay.spriteDPad.drawAFrame(MazeEscape.mainCanvas, DEF.FRAME_BUTTON_RIGHT_NORMAL, SCREEN_WIDTH - (DEF.SELECTLEVEL_BACKGROUND_X + 5 + DEF.DPAD_WIDTH), SCREEN_HEIGHT / 2);
			else
				StateGameplay.spriteDPad.drawAFrame(MazeEscape.mainCanvas, DEF.FRAME_BUTTON_RIGHT_HIGHTLIGHT, SCREEN_WIDTH - (DEF.SELECTLEVEL_BACKGROUND_X + 5 + DEF.DPAD_WIDTH), SCREEN_HEIGHT / 2);

			if (MazeEscape.isTouchDrapInRect(DEF.SELECTLEVEL_BACKGROUND_X + DEF.SELECTLEVEL_BACKGROUND_W - SELECTLEVEL_BUTTON_W - 8, DEF.SELECTLEVEL_BACKGROUND_Y + 8, SELECTLEVEL_BUTTON_W, SELECTLEVEL_BUTTON_H))
				StateGameplay.spriteDPad.drawAFrame(MazeEscape.mainCanvas, DEF.FRAME_CANCEL_HIGHTLIGHT, DEF.SELECTLEVEL_BACKGROUND_X + DEF.SELECTLEVEL_BACKGROUND_W - SELECTLEVEL_BUTTON_W / 2 - 8, DEF.SELECTLEVEL_BACKGROUND_Y + SELECTLEVEL_BUTTON_H / 2 + 8);
			else
				StateGameplay.spriteDPad.drawAFrame(MazeEscape.mainCanvas, DEF.FRAME_CANCEL_NORMAL, DEF.SELECTLEVEL_BACKGROUND_X + DEF.SELECTLEVEL_BACKGROUND_W - SELECTLEVEL_BUTTON_W / 2 - 8, DEF.SELECTLEVEL_BACKGROUND_Y + SELECTLEVEL_BUTTON_H / 2 + 8);

			break;
		case MESSAGE_DTOR:
			break;
		}
	}
}
